{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "xy = np.loadtxt('C:\\\\Users\\\\korea\\\\Desktop\\\\test.csv', delimiter=',', dtype=np.float32)\n",
    "x_data=xy[:,0:-1]\n",
    "y_data=xy[:,[-1]]\n",
    "F_data=xy[:,:]\n",
    "\n",
    "# Model Inputs\n",
    "def model_inputs(real_dim, noise_dim):\n",
    "    inputs_real_ = tf.placeholder(tf.float32, shape=[None, real_dim], name='inputs_real')\n",
    "    inputs_z_ = tf.placeholder(tf.float32, shape=[None, noise_dim], name='inputs_z')\n",
    "    \n",
    "    return inputs_real_, inputs_z_\n",
    "\n",
    "def leaky_relu(x, alpha):\n",
    "    return tf.maximum(alpha * x, x)\n",
    "\n",
    "# Generator Network# Genera \n",
    "def model_generator(z_input, out_dim, n_units=128, reuse=False, alpha=0.01):\n",
    "    # used to reuse variables, name scope\n",
    "    with tf.variable_scope('generator', reuse=reuse):\n",
    "        hidden_layer = tf.layers.dense(z_input, n_units, activation=tf.nn.relu)\n",
    "        \n",
    "        logits = tf.layers.dense(hidden_layer, out_dim, activation=None)\n",
    "        outputs = tf.nn.tanh(logits)\n",
    "        \n",
    "        return outputs, logits\n",
    "\n",
    "# Discriminator Network\n",
    "def model_discriminator(input, n_units=128, reuse=False, alpha=0.01):\n",
    "    with tf.variable_scope('discriminator', reuse=reuse):\n",
    "        hidden_layer = tf.layers.dense(input, n_units, activation=None)\n",
    "        hidden_layer = leaky_relu(hidden_layer, alpha)\n",
    "        \n",
    "        logits = tf.layers.dense(hidden_layer, 1, activation=None)\n",
    "        outputs = tf.nn.sigmoid(logits)\n",
    "        \n",
    "        return outputs, logits\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "input_size = 39\n",
    "z_dim = 21\n",
    "g_hidden_size = 128\n",
    "d_hidden_size = 128\n",
    "alpha = 0.01\n",
    "smooth = 0.1\n",
    "learning_rate = 0.01\n",
    "\n",
    "tf.reset_default_graph()  # If we don't have this, as we call this block over and over, the graph gets bigger and bigger\n",
    "\n",
    "graph = tf.Graph()\n",
    "with graph.as_default():\n",
    "    inputs_real, inputs_z = model_inputs(input_size, z_dim)\n",
    "    \n",
    "    g_outputs, g_logits = model_generator(inputs_z, input_size, n_units=g_hidden_size, reuse=False, alpha=alpha)\n",
    "    \n",
    "    d_outputs_real, d_logits_real = model_discriminator(inputs_real, n_units=d_hidden_size, reuse=False, alpha=alpha)\n",
    "    d_outputs_fake, d_logits_fake = model_discriminator(g_outputs, n_units=d_hidden_size, reuse=True, alpha=alpha)\n",
    "    \n",
    "    d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_real, labels=tf.ones_like(d_logits_real) * (1-smooth)))\n",
    "    d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.zeros_like(d_logits_fake)))\n",
    "    \n",
    "    d_loss = d_loss_real + d_loss_fake\n",
    "    g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.ones_like(d_logits_fake)))\n",
    "    \n",
    "    t_vars = tf.trainable_variables()\n",
    "    g_vars = [variable for variable in t_vars if 'generator' in variable.name]\n",
    "    d_vars = [variable for variable in t_vars if 'discriminator' in variable.name]\n",
    "    \n",
    "    # Affected Variables with var_list\n",
    "    d_optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(d_loss, var_list=d_vars)\n",
    "    g_optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(g_loss, var_list=g_vars)\n",
    "    \n",
    "    # Saving variables with var_list\n",
    "    saver = tf.train.Saver(var_list=g_vars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 1 / 10000 Complete. D_Loss : 2.885, G_Loss : 0.649\n",
      "step 101 / 10000 Complete. D_Loss : 23.903, G_Loss : 7.514\n",
      "step 201 / 10000 Complete. D_Loss : 13.317, G_Loss : 3.086\n",
      "step 301 / 10000 Complete. D_Loss : 22.142, G_Loss : 4.670\n",
      "step 401 / 10000 Complete. D_Loss : 24.342, G_Loss : 4.071\n",
      "Training Complete. \n"
     ]
    }
   ],
   "source": [
    "samples = []\n",
    "with tf.Session(graph=graph) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    for step in range(500):\n",
    "        \n",
    "        batch_images = F_data[step].reshape([1, 39])\n",
    "        batch_z = np.random.uniform(-1, 1, size=[1, z_dim])\n",
    "        \n",
    "        _ = sess.run(d_optimizer, feed_dict={inputs_real : batch_images, inputs_z : batch_z})\n",
    "        _ = sess.run(g_optimizer, feed_dict={inputs_z : batch_z})\n",
    "        loss_d, loss_g = sess.run([d_loss, g_loss], feed_dict={inputs_real : batch_images, inputs_z : batch_z})\n",
    "        if step%100==0:\n",
    "            print('step {} / {} Complete. D_Loss : {:0.3f}, G_Loss : {:0.3f}'.format(step+1, 10000, loss_d, loss_g))\n",
    "        \n",
    "        sample_z = np.random.uniform(-1, 1, size=[1, z_dim])  # 16 Samples each epoch\n",
    "        gen_samples, _ = sess.run(model_generator(inputs_z, input_size, reuse=True), feed_dict={inputs_z : sample_z})\n",
    "\n",
    "        samples.append(gen_samples)\n",
    "\n",
    "    print('Training Complete. ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step:     0\tLoss: 34199.516\tAcc: 80.05%\n",
      "step:   100\tLoss: 34439.152\tAcc: 80.04%\n",
      "step:   200\tLoss: 34601.102\tAcc: 80.04%\n",
      "step:   300\tLoss: 34723.578\tAcc: 80.05%\n",
      "step:   400\tLoss: 34822.129\tAcc: 80.05%\n",
      "step:   500\tLoss: 34904.586\tAcc: 80.05%\n",
      "step:   600\tLoss: 34975.441\tAcc: 80.06%\n",
      "step:   700\tLoss: 35037.629\tAcc: 80.06%\n",
      "step:   800\tLoss: 35093.016\tAcc: 80.05%\n",
      "step:   900\tLoss: 35142.949\tAcc: 80.05%\n",
      "training by gan sample\n",
      "true=20163 false: 5029 acc: 0.8003731343283582\n"
     ]
    }
   ],
   "source": [
    "#xy = np.loadtxt('C:\\\\Users\\\\SANHA\\\\Desktop\\\\test.csv', delimiter=',', dtype=np.float32)\n",
    "#x_data=xy[:,0:-1]\n",
    "#y_data=xy[:,[-1]]\n",
    "#F_data=xy[:,:]\n",
    "\n",
    "x1_data=gen_samples[:,0:-1]\n",
    "y1_data=gen_samples[:,[-1]]\n",
    "\n",
    "\n",
    "\n",
    "nb_classes=2\n",
    "\n",
    "X=tf.placeholder(tf.float32,[None,38])\n",
    "Y=tf.placeholder(tf.int32,[None,1])\n",
    "\n",
    "Y_one_hot=tf.one_hot(Y,nb_classes)\n",
    "Y_one_hot=tf.reshape(Y_one_hot,[-1,nb_classes])\n",
    "\n",
    "W=tf.Variable(tf.random_normal([38,nb_classes]),name='weight')\n",
    "b=tf.Variable(tf.random_normal([nb_classes]),name='bias')\n",
    "\n",
    "logits=tf.matmul(X,W)+b\n",
    "hypothesis=tf.nn.softmax(logits)\n",
    "\n",
    "cost_i=tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=Y_one_hot)\n",
    "\n",
    "cost=tf.reduce_mean(cost_i)\n",
    "optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)\n",
    "\n",
    "prediction=tf.argmax(hypothesis,1) #가능성을 퍼센트로~~\n",
    "correct_prediction=tf.equal(prediction,tf.arg_max(Y_one_hot,1))\n",
    "accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    \n",
    "    for step in range(1000):\n",
    "        sess.run(optimizer,feed_dict={X:x1_data,Y:y1_data})\n",
    "        if step %100==0:\n",
    "            loss,acc=sess.run([cost,accuracy],feed_dict={X:x_data,Y:y_data})\n",
    "            print(\"step: {:5}\\tLoss: {:.3f}\\tAcc: {:.2%}\".format(step,loss,acc))\n",
    " \n",
    "    print(\"training by gan sample\")\n",
    "  \n",
    "    tr=0\n",
    "    fa=0\n",
    "    total=0\n",
    "\n",
    "\n",
    "    pred = sess.run(prediction, feed_dict={X: x_data})\n",
    "    for p, y in zip(pred, y_data.flatten()):\n",
    "            if(p==int(y)):\n",
    "                tr=tr+1\n",
    "            else:\n",
    "                fa=fa+1\n",
    "            #print(\"[{}] Prediction: {} Real Y: {}\".format(p == int(y), p, int(y)))\n",
    "\n",
    "    print(\"true={} false: {} acc: {}\".format(tr,fa,tr/(tr+fa)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 2, 0, ..., 0, 0, 0], dtype=int64)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:tensorflow]",
   "language": "python",
   "name": "conda-env-tensorflow-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
